package com.kfm.mybatis.mapper;

import com.kfm.mybatis.model.Account;
import com.kfm.mybatis.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IUserMapper {

    @Select("select * from user where username = #{username} and password = #{password}")
    @Results(value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password")
    })
    User login(User user);


    @Select("select * from user")
    @Results(id = "userMap", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password")
    })
    User login2();



    @Select("select * from user where id = #{id}")
    @Results(value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            // collection 多映射, 给 accountList 字段赋值
            @Result(property = "accountList", column = "id"
                    , many = @Many(select = "com.kfm.mybatis.mapper.IAccountMapper.selectByUid")),
    })
    User selectAll(Integer id);


    @Select("select a.id, a.money, a.uid, u.id user_id, u.username, u.password " +
        " from account a right join user u on a.uid = u.id " +
        "where u.id = #{id}")
    @Results(id = "userWithAccountsMap", value = {
            @Result(id = true, property = "id", column = "user_id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            // collection 多映射, 给 accountList 字段赋值
            @Result(property = "accountList"
                    ,  many = @Many(resultMap = "com.kfm.mybatis.mapper.IAccountMapper.accountMap")),
    })
    User selectAll2(Integer id);

    // 所有用户信息及其账户信息
    @Select("select t1.id user_id, username, password, t2.id, uid, money from user t1 left join account t2 on t1.id = t2.uid where t1.id < 4")
    @ResultMap("userWithAccountsMap")
    List<User> select();

}
